import { draw }from  "replicad";
import initWorker from "./cadWorker.js?worker";
import { wrap } from "comlink";
import { initScene } from "./components/initScene";
import { BufferGeometry, LineBasicMaterial, LineSegments, Mesh, MeshStandardMaterial, Vector3 } from "three";
import { syncFaces,syncLines,syncLinesFromFaces } from "replicad-threejs-helper";

const cadWorker = wrap(new  initWorker());
const faceMat = new MeshStandardMaterial({
  color: 0x00ff00,
  // transparent: true,
  // opacity: 0.5,
  side: 2,
});
const lineMat = new LineBasicMaterial({
  color: 0x0000ff,
})

const body = new BufferGeometry(),lines = new BufferGeometry();
const stage = initScene({
  cameraPosition: new Vector3(20,40,50),
  directLight:1,ambientLight:.5,
  antialias:true
});

cadWorker.createMesh(5).then((mesh) => {
  console.log(mesh);
  const startTime = performance.now() 
   if (mesh.faces) syncFaces(body, mesh.faces);
   if(mesh.edges){syncLines(lines, mesh.edges);}
   else if (mesh.faces){syncLinesFromFaces(lines, body);}
  
   console.log("sync took",performance.now() - startTime)
   const line  = new LineSegments(lines,lineMat);
   const mesh1 = new Mesh(body, faceMat);
   stage.scene.add(mesh1);
   stage.scene.add(line);
})



document.body.appendChild(stage.renderer.domElement);
stage.resize();
stage.loopRender();
